//
//  CouponCell.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/9/7.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "CouponCell.h"
@interface CouponCell()
@property(nonatomic,strong)UIImageView * backgroundImageView;

/**即将过期图片*/
@property(nonatomic,strong)UIImageView * overdueImageView;



@end
@implementation CouponCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.cellHeight = Coupon_height(86);
        UIImageView * backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dashed_frame"]];
        self.backgroundImageView = backgroundImageView;
        [self.contentView addSubview:backgroundImageView];
        self.backgroundView = backgroundImageView;
        
        
        self.overdueImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"past_due"]];
        [backgroundImageView addSubview:self.overdueImageView];
        
        self.deductibleLabel = [UILabel labelWithTextColor:HexColor(@"#333333") font:[UIFont systemFontOfSize:30]];
        [backgroundImageView addSubview:self.deductibleLabel];
        
        self.thresholdLabel = [UILabel labelWithTextColor:HexColor(@"#666666") font:[UIFont systemFontOfSize:12]];
        [backgroundImageView addSubview:self.thresholdLabel];
        
        self.shopnameLabel = [UILabel labelWithTextColor:HexColor(@"#333333") font:[UIFont systemFontOfSize:16]];
        [backgroundImageView addSubview:self.shopnameLabel];
        
        self.typeLabel = [UILabel labelWithTextColor:HexColor(@"#999999") font:[UIFont systemFontOfSize:10]];
        [backgroundImageView addSubview:self.typeLabel];
        
        self.dateLabel = [UILabel labelWithTextColor:HexColor(@"#666666") font:[UIFont systemFontOfSize:9]];
        [backgroundImageView addSubview:self.dateLabel];
        
        self.unUseLabel = [UILabel labelWithTextColor:HexColor(@"#666666") font:[UIFont systemFontOfSize:11]];
        self.unUseLabel.text = @"未使用";
        [backgroundImageView addSubview:self.unUseLabel];
        
        self.detailButton = [[UIButton  alloc] initWithFrame:CGRectZero];
        self.detailButton.titleLabel.font = [UIFont systemFontOfSize:9];
        [self.detailButton setTitleColor:HexColor(@"#666666") forState:UIControlStateNormal];
        [self.detailButton setTitle:@"详情" forState:UIControlStateNormal];
        [self.detailButton setImage:[UIImage imageNamed:@"Small-round"] forState:UIControlStateNormal];
        self.detailButton.width = [self.detailButton.currentTitle sizeInAre:MaxSize font:self.detailButton.titleLabel.font].width+self.detailButton.currentImage.size.width + 3;
        self.detailButton.height = self.detailButton.currentImage.size.height;
        [backgroundImageView addSubview:self.detailButton];
        
        [self setDeductible:50 threshold:400 shopName:@"善觉堂官方旗舰店" type:@"通用券" date:@"2016.06.01-2016.07.01 到期" soonExpire:arc4random()%2];
        
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat margin = 4;
    self.backgroundImageView.width = ScreenWidth-2*globalMargin;
    self.backgroundImageView.height = self.cellHeight;
    self.backgroundImageView.x = globalMargin;
    self.backgroundImageView.y = 0;
    
    
    
    self.detailButton.x = self.backgroundView.width - self.detailButton.width - Coupon_width(24);
    self.detailButton.y = self.backgroundView.height - self.detailButton.height - Coupon_height(8);
    self.detailButton.titleEdgeInsets = UIEdgeInsetsMake(0, -(self.detailButton.width - self.detailButton.currentImage.size.width), 0, 0);
    self.detailButton.imageEdgeInsets = UIEdgeInsetsMake(0, (self.detailButton.width - self.detailButton.currentImage.size.width), 0, 0);
    
    self.deductibleLabel.centerX = Coupon_width(138)*0.5;
    self.deductibleLabel.centerY = self.cellHeight*0.5;
    
    self.thresholdLabel.size = [self.thresholdLabel.text sizeInAre:MaxSize font:self.thresholdLabel.font];
    self.thresholdLabel.centerX = self.deductibleLabel.centerX;
    self.thresholdLabel.y = self.deductibleLabel.botton+margin;
    
    self.shopnameLabel.size = [self.shopnameLabel.text sizeInAre:MaxSize font:self.shopnameLabel.font];
    self.shopnameLabel.x = Coupon_width(138) + globalMargin;
    self.shopnameLabel.y = Coupon_height(25);
    
    self.typeLabel.size = [self.typeLabel.text sizeInAre:MaxSize font:self.typeLabel.font];
    self.typeLabel.x = self.shopnameLabel.x;
    self.typeLabel.y = self.shopnameLabel.botton + margin;
    
    self.dateLabel.size = [self.dateLabel.text sizeInAre:MaxSize font:self.dateLabel.font];
    self.dateLabel.x = self.shopnameLabel.x;
    self.dateLabel.centerY = self.detailButton.centerY;
    
    self.unUseLabel.size = [self.unUseLabel.text sizeInAre:MaxSize font:self.unUseLabel.font];
    self.unUseLabel.x = globalMargin;
    self.unUseLabel.y = globalMargin;
    
    if(self.soonExpire){
        self.overdueImageView.hidden = NO;
        self.unUseLabel.hidden = YES;
    }else{
        self.overdueImageView.hidden = YES;
        self.unUseLabel.hidden = NO;
    }
}

-(void)setDeductible:(CGFloat)deductible threshold:(CGFloat)threshold shopName:(NSString*)shopName type:(NSString*)type date:(NSString*)date soonExpire:(BOOL)soonExpire{
    NSMutableAttributedString *deductibleMst = [[NSMutableAttributedString alloc] initWithString:[[NSString stringWithFormat:@"%.0f",deductible] addHeader:@"¥"]];
    [deductibleMst addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} range:NSMakeRange(0, 1)];
    [deductibleMst addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30]} range:NSMakeRange(1, deductibleMst.length-1)];
    self.deductibleLabel.attributedText = deductibleMst;
    self.deductibleLabel.size = deductibleMst.size;
    
    self.thresholdLabel.text = [NSString stringWithFormat:@"满%.0f可用",threshold];
    self.shopnameLabel.text = shopName;
    self.typeLabel.text = type;
    self.dateLabel.text = date;
    
    self.soonExpire = soonExpire;
    
    [self setNeedsLayout];
}
@end
